package hong.jun.task;

import com.alibaba.fastjson.JSONObject;
import hong.jun.util.HttpUtil;

import java.util.HashMap;

/**
 * 说明:nft接口测试线程
 * Created on 2023/10/19 15:12
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class ThreadNftBoxTask extends ThreadTask {

    private int idx;

    private long startTime;

    Runnable task1;

    public ThreadNftBoxTask(int idx, long startTime) {
        this.idx = idx;
        this.startTime = startTime;
    }

    /**
     * @Description:
     * @Param: []
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/19
     */
    @Override
    protected void action() {
        try {
            if (task1 != null) {
                task1.run();
            }
            String threadName = Thread.currentThread().getName();
            long time1 = System.currentTimeMillis();
            //todo 发送接口
            long salt = boxOpen("测试用的钱包地址", "COMMON", "1", false);
            if (salt > 0) {
                String success = boxOpenNotify(salt, "SUCCESS", "");
                System.out.println("开盲盒通知结果:" + success);
            }
            long time2 = System.currentTimeMillis();
            System.out.printf("线程名:%s 执行 idx:%s 开盲盒耗时%s%n", threadName, idx, time2 - time1);
            System.out.printf("线程名:%s 总耗时%s %n", threadName, time2 - startTime);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("线程执行错误");
        }
    }


    /**
     * 去nft服务器开盲盒 todo 测试代码
     *
     * @param address
     * @param userType
     * @param mysteryBoxId
     * @param enable
     */
    public long boxOpen(String address, String userType, String mysteryBoxId, boolean enable) {
        String uri = "https://game-api-pre.lastodyssey.io/nftServer/box/open";
        JSONObject object = new JSONObject();
        object.put("address", address);
        object.put("user_type", userType);
        object.put("mystery_box_id", mysteryBoxId);
        object.put("ensure", String.valueOf(enable));
        try {
            long startTime = System.currentTimeMillis();
            String res = HttpUtil.postJSON(uri, object.toString(), null);
            JSONObject jsonObject = JSONObject.parseObject(res);
            long endTime = System.currentTimeMillis();
            System.out.println("开盲盒结果:" + jsonObject.toJSONString());
            JSONObject data = jsonObject.getJSONObject("data");
            return data.getLongValue("salt");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 去nft服务器通知开盲盒结果 todo 测试代码
     *
     * @param salt
     * @param status
     * @return
     */
    public String boxOpenNotify(long salt, String status, String signature) {
        String uri = "https://game-api-pre.lastodyssey.io/nftServer/box/open/notify";
        JSONObject object = new JSONObject();
        object.put("salt", salt);
        object.put("status", status);
        //签名
        HashMap<String, String> param = new HashMap<>();
        param.put("signature", signature);
        try {
            String res = HttpUtil.postJSON(uri, object.toString(), param);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
